conf = require("../config")
if not conf.gitsigns.enabled then return {} end

return {
  "lewis6991/gitsigns.nvim",
  version = "*",
  event = { 'BufReadPost', 'BufNewFile' },
  config = function ()
    require('gitsigns').setup({
      signs = {
        add          = { text = '┃' },
        change       = { text = '┃' },
        delete       = { text = '_' },
        topdelete    = { text = '‾' },
        changedelete = { text = '~' },
        untracked    = { text = '┆' },
      },
      signcolumn = true,  -- Toggle with `:Gitsigns toggle_signs`
      numhl      = false, -- Toggle with `:Gitsigns toggle_numhl`
      linehl     = false, -- Toggle with `:Gitsigns toggle_linehl`
      word_diff  = false, -- Toggle with `:Gitsigns toggle_word_diff`
      watch_gitdir = {
        follow_files = true
      },
      auto_attach = true,
      attach_to_untracked = false,
      current_line_blame = false, -- Toggle with `:Gitsigns toggle_current_line_blame`
      current_line_blame_opts = {
        virt_text = true,
        virt_text_pos = 'eol', -- 'eol' | 'overlay' | 'right_align'
        delay = 1000,
        ignore_whitespace = false,
        virt_text_priority = 100,
        use_focus = true,
      },
      current_line_blame_formatter = '<author>, <author_time:%R> - <summary>',
      sign_priority = 6,
      update_debounce = 100,
      status_formatter = nil, -- Use default
      max_file_length = 40000, -- Disable if file is longer than this (in lines)
      preview_config = {
        -- Options passed to nvim_open_win
        border = 'single',
        style = 'minimal',
        relative = 'cursor',
        row = 0,
        col = 1
      },
      on_attach = function(bufnr)
        local gitsigns = require('gitsigns')

        local function map(mode, l, r, opts)
          opts = opts or {}
          opts.buffer = bufnr
          vim.keymap.set(mode, l, r, opts)
        end

        -- Navigation
        map('n', ']c', function()
          if vim.wo.diff then
            vim.cmd.normal({']c', bang = true})
          else
            gitsigns.nav_hunk('next')
          end
        end, {desc = "next gitsign"})

        map('n', '[c', function()
          if vim.wo.diff then
            vim.cmd.normal({'[c', bang = true})
          else
            gitsigns.nav_hunk('prev')
          end
        end, {desc = "previous gitsign"})

        -- Actions
        map('n', '<space>hs', gitsigns.stage_hunk, { desc = "git toggle stage hunk" })
        map('n', '<space>hr', gitsigns.reset_hunk, { desc = "git reset hunk" })
        map('v', '<space>hs', function() gitsigns.stage_hunk {vim.fn.line('.'), vim.fn.line('v')} end)
        map('v', '<space>hr', function() gitsigns.reset_hunk {vim.fn.line('.'), vim.fn.line('v')} end)
        map('n', '<space>hS', gitsigns.stage_buffer, { desc = "git stage buffer" })
        map('n', '<space>hu', gitsigns.undo_stage_hunk, { desc = "git undo stage hunk" })
        map('n', '<space>hR', gitsigns.reset_buffer, { desc = "git reset buffer" })
        map('n', '<space>hp', gitsigns.preview_hunk, { desc = "git preview hunk" })
        map('n', '<space>hb', function() gitsigns.blame_line{full=true} end, { desc = "git blame line" })
        map('n', '<space>tb', gitsigns.toggle_current_line_blame, { desc = "Toggle git show blame line" })
        map('n', '<space>hd', gitsigns.diffthis, { desc = "git diff against index" })
        map('n', '<space>hD', function() gitsigns.diffthis('~') end, { desc = "git diff against index" })
        map('n', '<space>td', gitsigns.toggle_deleted, { desc = "Toggle git show deleted" })

        -- Text object
        map({'o', 'x'}, 'ih', ':<C-U>Gitsigns select_hunk<CR>')
      end,

    })
  end,
}

